package com.ygqh.baby.service.impl;

import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.mp.message.TemplateMessage;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.WxMessageType;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.po.*;
import com.ygqh.baby.service.*;
import com.ygqh.baby.service.wechat.api.YgTmplApi;
import com.ygqh.baby.service.wechat.message.YgTemplateMessage;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author admin
 */
@Service
public class WxSmallMessageServiceImpl extends BaseWxMpMessageService implements WxSmallMessageService {

    private final Logger logger = Logger.getLogger(BaseWxMpMessageService.class);
    private final ExecutorService executorService = Executors.newFixedThreadPool(2);

    @Autowired
    private YgUserAuthsService ygUserAuthsService;
    @Autowired
    private YgUserTemplateMessageSettingService ygUserTemplateMessageSettingService;
    @Autowired
    private YgOrderPaymentService ygOrderPaymentService;
    @Autowired
    private WxAttentionService wxAttentionService;

    @Value("${message.send}")
    private Boolean sendMsg;

    @Override
    public Message sendWaitingPay(YgOrder order, String samllOpenId) {
        String title = "亲，您还有需要付款的订单。订单提交后24小时内有效。";
        String templateId = "Z2NNoBwjLpWjU0DAeTaePsmJt3kCiNgsrZCmbihlKDI";
        String url = "pages/user/orderdetail/orderdetail?orderId=" + order.getOrderId();
        YgTemplateMessage tplMessage = new YgTemplateMessage(samllOpenId, templateId, title, url, order.getFormId());

        tplMessage.pushHead("#323232", title);
        tplMessage.pushItem("keyword1", "#323232", order.getOrderId().toString());
        tplMessage.pushItem("keyword2", "#323232", order.getProductNames() + " 等" + order.getItemCount() + "件");
        tplMessage.pushItem("keyword3", "#323232", "¥" + order.getTotalPrice().toString());
        return this.sendTmplMsg(tplMessage);
    }

    @Override
    public Message sendTmplMsg(YgTemplateMessage tplMessage) {
        if (tplMessage == null) {
            return Message.error("消息体为空", null);
        }
        return sendTmplMsg(tplMessage, tplMessage.getPlatformNo());
    }

    private Message sendTmplMsg(YgTemplateMessage tplMessage, String platformNo) {
        System.out.println("发送小程序模板消息：" + JSON.toJSONString(tplMessage));
        if (tplMessage == null) {
            return Message.error("消息体为空", null);
        }
        if (!sendMsg) {
            return Message.success(null);
        }
        TokenHolder tokenHolder = wxSmallAuthorityService.findTokenHolder(platformNo);
        YgTmplApi tmplApi = new YgTmplApi(tokenHolder);
        try {
            JsonResult sendResult = tmplApi.sendTmplMessage(tplMessage);
            if (sendResult.getCode() == 0) {
                return Message.success(sendResult.getText(), null);
            }

            return Message.error(sendResult.toString(), null);
        } catch (WeixinException e) {
            if (e.getMessage().contains("41029 form id used count reach limit hint")) {
                logger.warn("发送小程序模板消息失败；【" + JSON.toJSONString(tplMessage) + "】" + e.getMessage());
            } else {
                logger.error("发送小程序模板消息失败；【" + JSON.toJSONString(tplMessage) + "】" + e.getMessage());
            }
            e.printStackTrace();
            Message.error(e.getMessage());
        }
        return Message.success(null);
    }

    @Override
    public <T extends TemplateMessage> Message sendSmallTemplateMessage(T t) {
        if (!sendMsg) {
            return Message.success(null);
        }
        TokenHolder tokenHolder = wxSmallAuthorityService.findTokenHolder();
        YgTmplApi tmplApi = new YgTmplApi(tokenHolder);
        try {
            JsonResult sendResult = tmplApi.sendTmpleteMessage(t);
            if (sendResult.getCode() == 0) {
                return Message.success(sendResult.getText(), null);
            }

            return Message.error(sendResult.toString(), null);
        } catch (WeixinException e) {
            if (e.getMessage().contains("41029 form id used count reach limit hint")) {
                logger.warn("发送小程序模板消息失败；openId=【" + t.getToUser() + "】【" + t.getHead() + "】" + e.getMessage());
            } else {
                logger.error("发送小程序模板消息失败；openId=【" + t.getToUser() + "】【" + t.getHead() + "】" + e.getMessage());
            }
            e.printStackTrace();
            Message.error(e.getMessage());
        }
        return Message.success(null);
    }

    @Override
    public Message sendGroupOrderRemindMsg(String title, String productName, Long lastAmount, Date endTime, String prompt, Long orderId, String formId,
                                           String url, Long userId) {

        YgUser user = ygUserService.findById(userId);

        return this.sendGroupOrderRemindMsg(title, productName, lastAmount, endTime, prompt, orderId, formId, url, user.getSmallOpenId());
    }

    @Override
    public Message sendGroupOrderRemindMsg(String title, String productName, Long lastAmount, Date endTime, String prompt, Long orderId, String formId,
                                           String url, String openId) {
        String tempId = this.getWxMsgTempId(WxMessageType.SmallGroupOrderRemind);
        YgTemplateMessage tplMessage = new YgTemplateMessage(openId, tempId, title, url, formId);
        tplMessage.pushHead("#323232", title);
        tplMessage.pushItem("keyword1", "#323232", productName);
        tplMessage.pushItem("keyword2", "#323232", lastAmount.toString());
        tplMessage.pushItem("keyword3", "#323232", DateConvertUtils.formatDateTime(endTime));
        tplMessage.pushItem("keyword4", "#323232", prompt);

        return this.sendSmallTemplateMessage(tplMessage);
    }

    @Override
    public Message sendGiftReceiveSuccessMsg(String title, String productName, Date receiveTime, String receiver, String formId, String url, String openId) {
        String tempId = this.getWxMsgTempId(WxMessageType.SmallGiftReceiveSuccess);
        System.out.println("礼品领取成功通知tempId-->" + tempId);
        YgTemplateMessage tplMessage = new YgTemplateMessage(openId, tempId, title, url, formId);
        tplMessage.pushHead("#323232", title);
        tplMessage.pushItem("keyword1", "#323232", productName);
        tplMessage.pushItem("keyword2", "#323232", DateConvertUtils.formatDateTime(receiveTime));
        tplMessage.pushItem("keyword3", "#323232", receiver);
        return this.sendSmallTemplateMessage(tplMessage);
    }

    /**
     * 、小程序用户，下单后推送消息：
     * 进群丨咨询更方便、上新早知道、享受专属福利！
     * 链接 http://m.yiigoo.com/static/1534417413.html?sourceCode=mbxxjq
     * 备用文案：长短各一
     * 福利→邀请您进入壹果妈妈群，咨询更方便、上新早知道、享受专属福利！
     * 【进群】咨询更方便、上新早知道、发券！
     */
    @Override
    public Message sendQunMsg(String formId, String url, String remark, String openId) {
        String tempId = this.getWxMsgTempId(WxMessageType.SmallQun);
        if (StringUtil.isBlank(url)) {
            url = "pages/webview/webview?url=https://m.yiigoo.com/static/1534417413.html?sourceCode=mbxxjq";
        }
        YgTemplateMessage tplMessage = new YgTemplateMessage(openId, tempId, "", url, formId);
        if (StringUtil.isBlank(remark)) {
            remark = "进群丨咨询更方便、上新早知道、享受专属福利！";
        }
        tplMessage.pushItem("keyword1", "#323232", "壹果");
        tplMessage.pushItem("keyword2", "#323232", DateConvertUtils.formatDateTime(new Date()));
        tplMessage.pushItem("keyword3", "#323232", remark);
        return this.sendSmallTemplateMessage(tplMessage);
    }

    @Override
    public Message sendWaitingPayMessage(YgOrder order) {
        if (!sendMsg) {
            return null;
        }
        if (Constant.YIIGOO_PLATFORM_NO.equals(order.getPlatformNo())) {
            // 壹果的店 暂未开放该通知
            return Message.error("【壹果的店】暂未开放该通知", null);
        }
        try {
            String templateId = getSmallMsgTempId(WxMessageType.OrderWaitingPay, order.getPlatformNo());
            boolean sendAble = ygUserTemplateMessageSettingService.
                    sendAble(order.getUserId(), templateId, order.getPlatformNo(), AppType.Small);
            if (!sendAble) {
                return Message.error("用户已禁用【待付款提醒】通知", null);
            }
            String openId = ygUserAuthsService.getOpenId(order.getUserId(), order.getPlatformNo(), AppType.Small);
            String title = "待付款提醒";
            String url = "pages/user/orderdetail/orderdetail?orderId=" + order.getOrderId();
            YgTemplateMessage tplMessage = new YgTemplateMessage(openId, templateId, title, url, order.getFormId());
            tplMessage.setPlatformNo(order.getPlatformNo());
            tplMessage.pushItem("keyword1", "#323232", "¥" + order.getTotalPrice().toString());
            tplMessage.pushItem("keyword2", "#323232", order.getProductNames() + " 等" + order.getItemCount() + "件");
            tplMessage.pushItem("keyword3", "#323232", order.getReceiver());
            tplMessage.pushItem("keyword4", "#323232", order.getOrderId().toString());
            tplMessage.pushItem("keyword5", "#323232", DateConvertUtils.formatDateTime(order.getCreateTime()));
            tplMessage.pushItem("keyword6", "#323232", "请在" +
                    DateConvertUtils.formatDateTime(DateConvertUtils.addDay(order.getCreateTime(), 1)) + "之前完成支付");
            return sendTmplMsg(tplMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(e.getMessage(), null);
        }
    }

    @Override
    public Message sendOrderPaySuccessMsg(YgOrder order, String formId) {
        if (!sendMsg) {
            return null;
        }
        if (Constant.YIIGOO_PLATFORM_NO.equals(order.getPlatformNo())) {
            // 壹果的店 暂未开放该通知
            return Message.error("【壹果的店】暂未开放该通知", null);
        }
        try {
            String templateId = getSmallMsgTempId(WxMessageType.OrderPaySuccess, order.getPlatformNo());
            boolean sendAble = ygUserTemplateMessageSettingService.
                    sendAble(order.getUserId(), templateId, order.getPlatformNo(), AppType.Small);
            if (!sendAble) {
                return Message.error("用户已禁用【付款成功通知】通知", null);
            }
            String openId = ygUserAuthsService.getOpenId(order.getUserId(), order.getPlatformNo(), AppType.Small);
            String title = "付款成功通知";
            String url = "pages/user/orderdetail/orderdetail?orderId=" + order.getOrderId();
            YgTemplateMessage tplMessage = new YgTemplateMessage(openId, templateId, title, url, formId);
            tplMessage.setPlatformNo(order.getPlatformNo());
            tplMessage.pushItem("keyword1", "#323232", "¥" + order.getTotalPrice().toString());
            String productNames = order.getOrderDetail().get(0).getProductName();
            Long itemCount = order.getOrderDetail().stream().map(YgOrderDetail::getQuantity).reduce((q1, q2) -> q1 + q2).get();
            tplMessage.pushItem("keyword2", "#323232", productNames + " 等" + itemCount + "件");
            return sendTmplMsg(tplMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(e.getMessage(), null);
        }
    }


    @Override
    public void sendOrderPaySuccessMsgAsync(YgOrder order, String formId) {
        executorService.execute(() -> {
            // 考虑到预支付id的有效性，延迟8秒发送支付成功通知
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sendOrderPaySuccessMsg(order, formId);
        });
    }

    @Override
    public YgTemplateMessage getOrderSendMsgByPlatform(YgOrder order, String logisticsName, String logisticsNo) {
        if (!sendMsg) {
            return null;
        }
        try {
            if (Constant.YIJIADIAN_PLATFORM_NO.equals(order.getPlatformNo())) {
                // 壹果的店 暂未开放该通知
                String templateId = getSmallMsgTempId(WxMessageType.OrderSendMsg, order.getPlatformNo());
                boolean sendAble = ygUserTemplateMessageSettingService.
                        sendAble(order.getUserId(), templateId, order.getPlatformNo(), AppType.Small);
                if (!sendAble) {
                    return null;
                }
                String openId = ygUserAuthsService.getOpenId(order.getUserId(), order.getPlatformNo(), AppType.Small);
                String title = "订单发货提醒";
                String url = "pages/user/orderdetail/orderdetail?orderId=" + order.getOrderId();
                YgOrderPayment payment = ygOrderPaymentService.findByOrderId(order.getOrderId());
                YgTemplateMessage tplMessage = new YgTemplateMessage(openId, templateId, title, url, payment.getPrepayId());
                String productNames = order.getOrderDetail().get(0).getProductName();
                Long itemCount = order.getOrderDetail().stream().map(YgOrderDetail::getQuantity).reduce((q1, q2) -> q1 + q2).get();
                tplMessage.pushItem("keyword1", "#323232", productNames + " 等" + itemCount + "件");
                tplMessage.pushItem("keyword2", "#323232", logisticsName);
                tplMessage.pushItem("keyword3", "#323232", logisticsNo);
                tplMessage.pushItem("keyword4", "#323232", order.getReceiver());
                tplMessage.pushItem("keyword5", "#323232", order.getOrderId().toString());
                tplMessage.setPlatformNo(order.getPlatformNo());
                return tplMessage;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Message sendGroupOrderRemindMsg(String productName, Long lastAmount, Date completeTime, Long orderId,
                                           String formId, Long userId, String platformNo) {
        if (!sendMsg) {
            return Message.success(null);
        }
        try {
            YgTemplateMessage tplMessage = null;
            if (Constant.YIIGOO_PLATFORM_NO.equals(platformNo)) {
                // 壹果的店
                String title = "亲，您的拼团商品 " + productName + " 件数还不够哦。";
                String url = "pages/user/orderdetail/orderdetail?orderId=" + orderId;
                YgUser user = ygUserService.findById(userId);
                String tempId = this.getWxMsgTempId(WxMessageType.SmallGroupOrderRemind);
                tplMessage = new YgTemplateMessage(user.getSmallOpenId(), tempId, title, url, formId);
                tplMessage.pushHead("#323232", title);
                tplMessage.pushItem("keyword1", "#323232", productName);
                tplMessage.pushItem("keyword2", "#323232", lastAmount.toString());
                tplMessage.pushItem("keyword3", "#323232", DateConvertUtils.formatDateTime(completeTime));
                tplMessage.pushItem("keyword4", "#323232", "快去分享给您的好友一起拼团吧.");
            } else if (Constant.YIJIADIAN_PLATFORM_NO.equals(platformNo)) {
                // 壹家店
                String templateId = getSmallMsgTempId(WxMessageType.GroupOrderCompleting, platformNo);
                boolean sendAble = ygUserTemplateMessageSettingService.
                        sendAble(userId, templateId, platformNo, AppType.Small);
                if (!sendAble) {
                    return null;
                }
                String openId = ygUserAuthsService.getOpenId(userId, platformNo, AppType.Small);
                String title = "拼团待成团提醒";
                String url = "pages/user/orderdetail/orderdetail?orderId=" + orderId;
                tplMessage = new YgTemplateMessage(openId, templateId, title, url, formId);
                tplMessage.pushItem("keyword1", "#323232", productName);
                tplMessage.pushItem("keyword2", "#323232", lastAmount + "人");
                tplMessage.pushItem("keyword3", "#323232", "参团人数不足，快快邀请好友参与吧！");
                tplMessage.pushItem("keyword4", "#323232", DateConvertUtils.formatDateTime(completeTime));
                tplMessage.setPlatformNo(platformNo);
            }
            return this.sendTmplMsg(tplMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(e.getMessage(), null);
        }
    }

    @Override
    public YgTemplateMessage getGroupOrderSuccessSmallMsg(Long userId, Long orderId, String productName,
                                                          BigDecimal orderPrice, String platformNo, Date completeTime) {
        if (!sendMsg) {
            return null;
        }
        YgTemplateMessage tplMessage = null;
        YgUser user = ygUserService.findById(userId);
        if (user == null || (Constant.YIIGOO_PLATFORM_NO.equals(platformNo) && StringUtil.isEmpty(user.getSmallOpenId()))) {
            return null;
        }
        YgOrderPayment orderPayment = ygOrderPaymentService.findByOrderId(orderId);
        if (orderPayment == null || !AppType.Small.equals(orderPayment.getAppType())) {
            return null;
        }
        try {
            if (Constant.YIIGOO_PLATFORM_NO.equals(platformNo)) {
                String title = "亲，恭喜您在壹果拼团成功哦。";
                String remark = "点击进入订单详情页，查看订单信息。";
                String path = "pages/user/orderdetail/orderdetail?orderId=" + orderId;
                String tempId = getWxMsgTempId(WxMessageType.SmallGroupOrderComplete);
                tplMessage = new YgTemplateMessage(user.getSmallOpenId(), tempId, title, path, orderPayment.getPrepayId());
                tplMessage.pushHead("#323232", title);
                tplMessage.pushItem("keyword1", "#323232", productName);
                tplMessage.pushItem("keyword2", "#323232", "¥" + orderPrice);
                tplMessage.pushItem("remark", "#323232", remark);
            } else if (Constant.YIJIADIAN_PLATFORM_NO.equals(platformNo)) {
                String templateId = getSmallMsgTempId(WxMessageType.GroupOrderComplete, platformNo);
                boolean sendAble = ygUserTemplateMessageSettingService.
                        sendAble(userId, templateId, platformNo, AppType.Small);
                if (!sendAble) {
                    return null;
                }
                String openId = ygUserAuthsService.getOpenId(userId, platformNo, AppType.Small);
                String title = "成团提醒";
                String url = "pages/user/orderdetail/orderdetail?orderId=" + orderId;
                tplMessage = new YgTemplateMessage(openId, templateId, title, url, orderPayment.getPrepayId());
                tplMessage.pushItem("keyword1", "#323232", productName);
                tplMessage.pushItem("keyword2", "#323232", DateConvertUtils.formatDateTime(completeTime));
                tplMessage.pushItem("keyword3", "#323232", "恭喜您，报名的团购活动已成团！");
                tplMessage.setPlatformNo(platformNo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tplMessage;
    }

    @Override
    public Message sendReturnMoneyMsg(String returnCode, BigDecimal refundPrice, String returnReason,
                                      Long userId, String platformNo, String formId) {
        if (!sendMsg) {
            return null;
        }
        if (!Constant.YIJIADIAN_PLATFORM_NO.equals(platformNo)) {
            // 壹果的店 暂未开放该通知
            return Message.error("暂未开放该通知", null);
        }
        try {
            String templateId = getSmallMsgTempId(WxMessageType.ReturnOrderCompleted, platformNo);
            boolean sendAble = ygUserTemplateMessageSettingService.
                    sendAble(userId, templateId, platformNo, AppType.Small);
            if (!sendAble) {
                return Message.error("用户已禁用【退款通知】通知", null);
            }
            String openId = ygUserAuthsService.getOpenId(userId, platformNo, AppType.Small);
            String title = "退款通知";
            String url = "pages/return/detail/detail?returnOrderId=" + returnCode;
            YgTemplateMessage tplMessage = new YgTemplateMessage(openId, templateId, title, url, formId);
            tplMessage.setPlatformNo(platformNo);
            tplMessage.pushItem("keyword1", "#323232", returnCode);
            tplMessage.pushItem("keyword2", "#323232", refundPrice + "元");
            tplMessage.pushItem("keyword3", "#323232", returnReason);
            tplMessage.pushItem("keyword4", "#323232", "您的退款订单已成功受理，金额将原路返还到您的微信支付账户");
            return sendTmplMsg(tplMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(e.getMessage(), null);
        }
    }

    @Override
    public Message sendIncomeReceiptMsg(Long userId, String platformNo, BigDecimal incomePayPrice, String orderId,
                                        BigDecimal orderPrice) {
        if (!sendMsg) {
            return null;
        }
        if (!Constant.YIJIADIAN_PLATFORM_NO.equals(platformNo)) {
            // 壹果的店 暂未开放该通知
            return Message.error("【壹果的店】暂未开放该通知", null);
        }
        try {
            String templateId = getSmallMsgTempId(WxMessageType.IncomeReceipt, platformNo);
            boolean sendAble = ygUserTemplateMessageSettingService.
                    sendAble(userId, templateId, platformNo, AppType.Small);
            if (!sendAble) {
                return Message.error("用户已禁用【收益到账通知】通知", null);
            }
            String openId = ygUserAuthsService.getOpenId(userId, platformNo, AppType.Small);
            if (StringUtils.isBlank(openId)) {
                return Message.error("openId不存在", null);
            }
            WxAttention attention = wxAttentionService.findByOpenId(openId);
            if (attention == null || StringUtils.isBlank(attention.getEvent())) {
                return Message.error("无可用formid", null);
            }
            String title = "收益到账通知";
            String url = "pages/agent/profit/profit";
            YgTemplateMessage tplMessage = new YgTemplateMessage(openId, templateId, title, url, attention.getEvent());
            tplMessage.setPlatformNo(platformNo);
            tplMessage.pushItem("keyword1", "#323232", incomePayPrice + "元");
            tplMessage.pushItem("keyword2", "#323232", orderId);
            tplMessage.pushItem("keyword3", "#323232", orderPrice.toString());
            tplMessage.pushItem("keyword4", "#323232", "预收入到账");
            tplMessage.pushItem("keyword5", "#323232", incomePayPrice.compareTo(BigDecimal.ZERO) > 0 ?
                    "商品佣金" : "商品退货");
            Message message = sendTmplMsg(tplMessage);
            wxAttentionService.updateUpdateTime(attention.getId());
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(e.getMessage(), null);
        }
    }

    @Override
    public Message sendInviteUserSuccessMsg(Long userId, String platformNo, String recommendNickName, Date recommendTime) {
        if (!sendMsg) {
            return null;
        }
        if (!Constant.YIJIADIAN_PLATFORM_NO.equals(platformNo)) {
            // 壹果的店 暂未开放该通知
            return Message.error("【壹果的店】暂未开放该通知", null);
        }
        try {
            String templateId = getSmallMsgTempId(WxMessageType.InviteUserSuccess, platformNo);
            boolean sendAble = ygUserTemplateMessageSettingService.
                    sendAble(userId, templateId, platformNo, AppType.Small);
            if (!sendAble) {
                return Message.error("用户已禁用【邀请成功通知】通知", null);
            }
            String openId = ygUserAuthsService.getOpenId(userId, platformNo, AppType.Small);
            if (StringUtils.isBlank(openId)) {
                return Message.error("openId不存在", null);
            }
            WxAttention attention = wxAttentionService.findByOpenId(openId);
            if (attention == null || StringUtils.isBlank(attention.getEvent())) {
                return Message.error("无可用formid", null);
            }
            String title = "邀请成功通知";
            String url = "pages/agent/fans/fans";
            YgTemplateMessage tplMessage = new YgTemplateMessage(openId, templateId, title, url, attention.getEvent());
            tplMessage.setPlatformNo(platformNo);
            tplMessage.pushItem("keyword1", "#323232", recommendNickName);
            tplMessage.pushItem("keyword2", "#323232", "已成为您的粉丝");
            tplMessage.pushItem("keyword3", "#323232", DateConvertUtils.formatDateTime(recommendTime));
            Message message = sendTmplMsg(tplMessage);
            wxAttentionService.updateUpdateTime(attention.getId());
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(e.getMessage(), null);
        }
    }


    @Override
    public Message sendTemplateMessage(TemplateMessage tplMessage) {
        YgTemplateMessage templateMessage = (YgTemplateMessage) tplMessage;
        return sendTmplMsg(templateMessage);
    }
}
